<template>
    <div>
        <h1 style="text-align:center;">登录</h1>
        <van-form @submit="onSubmit">
            <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]" />
            <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]" />
            <div style="margin: 16px;">
                <van-button round block type="info" color="orangered" native-type="submit">提交</van-button>
            </div>
        </van-form>
        <div style="text-align:center;">
            <a href="#/my/register">已有账号？去注册！</a>
        </div>
    </div>
</template>

<script setup>
// import Vue from 'vue';
import { Form } from 'vant';
import { Field } from 'vant';
import { Button } from 'vant';
import { login } from '@/request/request'
import { Toast } from 'vant';
import { setCookie } from '@/utils/cookie'
import { showSuccessToast, showFailToast } from 'vant';
import 'vant/es/toast/style';
import { useRouter, useRoute } from 'vue-router'
import { createApp, ref } from 'vue';
const app = createApp();
const router = useRouter()
const route = useRoute()
app.use(Toast);
app.use(Button);
app.use(Form);
app.use(Field);

let username = ref('')
let password = ref('')

let onSubmit = (values) => {
    console.log(values, 111);
    login(values).then(data => {
        if (data.error_code === 200) {
            //登录成功，储存cookie
            setCookie('username', values.username, 7200)
            //获取路径中url参数
            let url = route.query.url
            if (!url) {
                url = '/'
            }
            showSuccessToast({
                message: data.msg,
                onClose: () => {
                    router.push('/')
                }
            });
        } else {
            showFailToast(data.msg)
        }
    })
}

</script>

<style></style>
